package com.fzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.common.model.collection.param.CheckThisUserExpectWorkIsCollection;
import com.fzg.entity.CompanyUserCollection;
import com.fzg.entity.Enterprise;
import com.fzg.entity.UserJob;
import com.fzg.mapper.CompanyUserCollectionMapper;
import com.fzg.mapper.EnterpriseMapper;
import com.fzg.mapper.UserJobMapper;
import com.fzg.service.CompanyUserCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2022-03-25
 */
@Service
public class CompanyUserCollectionServiceImpl extends ServiceImpl<CompanyUserCollectionMapper, CompanyUserCollection> implements CompanyUserCollectionService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private CompanyUserCollectionMapper companyUserCollectionMapper;

    @Autowired
    private UserJobMapper userJobMapper;

    /**
     * 判断当前这个职位是否被收藏
     *
     * @param param 参数类
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper checkThisUserExpectWorkIsCollection(CheckThisUserExpectWorkIsCollection param) {
        Enterprise enterprise = enterpriseMapper.selectOne(new LambdaQueryWrapper<Enterprise>().eq(Enterprise::getAccountNumber, param.getAccount()));

        UserJob userJob = userJobMapper.selectById(param.getUserJobAid());
        CompanyUserCollection companyUserCollection = companyUserCollectionMapper.selectOne(new LambdaQueryWrapper<CompanyUserCollection>()
                .eq(CompanyUserCollection::getUserAid, userJob.getUseraid())
                .eq(CompanyUserCollection::getCompanyAid,enterprise.getAid()));

        if (companyUserCollection != null){
            return ResponseWrapper.markSuccess(true);
        }else {
            return ResponseWrapper.markSuccess(false);
        }

    }

    /**
     * 企业点击收藏
     *
     * @param param 参数类
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper companyCollection(CheckThisUserExpectWorkIsCollection param) {
        CompanyUserCollection companyUserCollection = new CompanyUserCollection();

        UserJob userJob = userJobMapper.selectById(param.getUserJobAid());

        Enterprise enterprise = enterpriseMapper.selectOne(new LambdaQueryWrapper<Enterprise>().eq(Enterprise::getAccountNumber, param.getAccount()));
        companyUserCollection.setCompanyAid(enterprise.getAid());

        if (userJob!=null){
            companyUserCollection.setUserAid(userJob.getUseraid());
            companyUserCollectionMapper.insert(companyUserCollection);
            return ResponseWrapper.markCustomSuccess("收藏成功！");
        }else {
            return ResponseWrapper.markCustomSuccess("收藏失败！");
        }

    }

    /**
     * 企业取消收藏用户
     *
     * @param param 参数类
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper cancelCollectUserExpectWork(CheckThisUserExpectWorkIsCollection param) {
        Enterprise enterprise = enterpriseMapper.selectOne(new LambdaQueryWrapper<Enterprise>().eq(Enterprise::getAccountNumber, param.getAccount()));

        UserJob userJob = userJobMapper.selectById(param.getUserJobAid());

        int delete = companyUserCollectionMapper.delete(new LambdaQueryWrapper<CompanyUserCollection>()
            .eq(CompanyUserCollection::getUserAid,userJob.getUseraid())
            .eq(CompanyUserCollection::getCompanyAid,enterprise.getAid()));

        if (delete > 0){
            return ResponseWrapper.markCustomSuccess("已取消收藏");
        }else {
            return ResponseWrapper.markError();
        }

    }
}
